/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: StaffController.java 47 2012-09-20 06:31:35Z zhuxiaopeng $
 * $Revision: 47 $
 * $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 * $Author: zhuxiaopeng $
 */
package daily.web.staff;

import static zhu.framework.web.WebUtility.*;

import java.util.*;

import javax.ejb.*;
import javax.enterprise.context.*;
import javax.faces.model.*;
import javax.inject.*;

import zhu.framework.service.*;
import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.entity.*;
import daily.service.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 47 $ - $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 */
@Named
@RequestScoped
public class StaffController extends ConversationalController {

	private enum ActionOutCome {
		EDIT, SAVE, CANCEL, DELETE
	}

	public String editStaff() {
		return ActionOutCome.EDIT.toString();
	}

	public String createStaff() {
		final Staff selectStaff = new Staff();
		selectStaff.setUsed(true);
		selectStaff.setStaffGroup(StaffType.USERS);
		staffConversation.setSelectStaff(selectStaff);
		return ActionOutCome.EDIT.toString();
	}

	public String deleteStaff() {
		return ActionOutCome.DELETE.toString();
	}

	public String saveStaff() {
		try {
			final Staff selectStaff = staffConversation.getSelectStaff();
			if (selectStaff.getEntityId() == null) {
				selectStaff.setPassword(selectStaff.getUserName());
			}
			staffService.saveStaff(selectStaff);
		} catch (final BusinessException e) {
			getWebRequestContext().addMessages(WebMessage.valueOf(e.getMessages()));
			return null;
		}
		staffConversation.setStaffList(null);
		return ActionOutCome.SAVE.toString();
	}

	public String delete() {
		final Staff selectStaff = staffConversation.getSelectStaff();
		staffService.deleteStaff(selectStaff);
		staffConversation.setStaffList(null);
		return ActionOutCome.SAVE.toString();
	}

	public String cancel() {
		return ActionOutCome.CANCEL.toString();
	}

	public List<Staff> getStaffList() {
		List<Staff> staffList = staffConversation.getStaffList();
		if (staffList == null) {
			staffList = staffService.getStaffList();
			staffConversation.setStaffList(staffList);
		}
		return staffList;
	}

	public List<SelectItem> getStaffGroupMenus() {
		final List<SelectItem> ret = new ArrayList<>();
		for (final StaffType group : StaffType.values()) {
			ret.add(new SelectItem(group, group.getLabel()));
		}
		return ret;
	}

	@Inject
	private StaffConversation staffConversation;

	@EJB
	private StaffServiceImpl staffService;
}
